package com.enzoism.springboot.dive.swap;

import com.alibaba.fastjson.JSON;
import com.enzoism.springboot.dive.model.UsrModel;

import java.util.*;
import java.util.stream.IntStream;

public class TestSwap {
    public static void main(String[] args) {
        /**
         * 进行数据索引替换
         */
        List<String> list = Arrays.asList("test1","test2","test3");
        System.out.println("更改前：" + JSON.toJSONString(list));
        Collections.swap(list, 1, 0);
        System.out.println("更改后：" + JSON.toJSONString(list));
        System.out.println();
        /**
         * 将指定属性的对象放在第一位返回
         */
        List<UsrModel> dataList = UsrModel.initList();
        System.out.println("更改前：" + JSON.toJSONString(dataList));
        List<UsrModel> usrModels = dataSort(dataList, "009");
        System.out.println("更改后：" + JSON.toJSONString(usrModels));


    }

    /**
     * 将指定属性的对象放在第一位返回
     */
    private static  List<UsrModel> dataSort(List<UsrModel> list,String usrId){
        OptionalInt optionalInt = IntStream.range(0, list.size()).filter(index -> list.get(index).getUsrId().equals(usrId)).findFirst();
        if (optionalInt.isPresent()){
            Collections.swap(list,optionalInt.getAsInt(),0);
        }
        return list;
    }
}
